<?php
/**
 * Created by PhpStorm.
 * User: LiJia
 * Date: 2015/10/26
 * Time: 14:46
 */

namespace app\modules\admin\models;
use Yii;
use yii\base\Model;

class AesManageForm extends Model{

    private $hex_iv = '01040105090200000000000000000605'; # 此处是转换java的byte[]一位转成两位高位填0
    private $aes_key = false; #Same as in JAVA
    function __construct($key) {
        $this->aes_key = hash('sha256', $key, true);


        //echo $this->aes_key.'<br/>';
   //     var_dump(hash('sha256', $key, true));
        //var_dump(chr(hexdec('0104')));
    }
    function encrypt($str) {

        $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');


        mcrypt_generic_init($td, $this->aes_key, $this->hexToStr($this->hex_iv));
        $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
       // var_dump($block);  //16
        $pad = $block - (strlen($str) % $block);
      //  var_dump($pad);// 15
        $str .= str_repeat(chr($pad), $pad);
      //  var_dump($str);
        $encrypted = mcrypt_generic($td, $str);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        return $this->urlsafe_b64encode($encrypted);
    }
    function decrypt($code) {
        $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
        mcrypt_generic_init($td, $this->aes_key, $this->hexToStr($this->hex_iv));
        $str = mdecrypt_generic($td, $this->urlsafe_b64decode($code));
        $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        return $this->strippadding($str);
    }
    /*
      For PKCS7 padding
     */
    private function addpadding($string, $blocksize = 16) {
        $len = strlen($string);
        $pad = $blocksize - ($len % $blocksize);
        $string .= str_repeat(chr($pad), $pad);
        return $string;
    }
    private function strippadding($string) {
        $slast = ord(substr($string, -1));
        $slastc = chr($slast);
        $pcheck = substr($string, -$slast);
        if (preg_match("/$slastc{" . $slast . "}/", $string)) {
            $string = substr($string, 0, strlen($string) - $slast);
            return $string;
        } else {
            return false;
        }
    }
    function hexToStr($hex)
    {
        $string='';
        for ($i=0; $i < strlen($hex)-1; $i+=2)
        {
            $string .= chr(hexdec($hex[$i].$hex[$i+1]));
        }
        return $string;
    }

    public function urlsafe_b64decode($string) {
        $data = str_replace(array('-','_'),array('+','/'),$string);
        $mod4 = strlen($data) % 4;
        if ($mod4) {
            $data .= substr('====', $mod4);
        }

        return base64_decode($data);

    }
    function urlsafe_b64encode($string) {
    $data = base64_encode($string);
    $data = str_replace(array('+','/','='),array('-','_',''),$data);
    return $data;

    }



}